package com.jieshuibao.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jieshuibao.model.db.Category;
import com.jieshuibao.model.domain.CategoryTreeResponse;
import com.jieshuibao.model.domain.CookieUser;
import com.jieshuibao.service.CategoryService;
import com.jieshuibao.service.RedisService;

@Controller
@RequestMapping(value="/category")
public class CategoryController {
	private static final Logger logger = LoggerFactory
			.getLogger("CategoryController.class");

	private static final ObjectMapper mapper = new ObjectMapper();
	@Resource
	private RedisService redisService;
	@Resource
	private CategoryService categoryService;
	//擅长业务树结构
	@RequestMapping(value = "/tree",method=RequestMethod.POST)
	@ResponseBody
	public Object categoryTree(HttpServletRequest request){
		
		CategoryTreeResponse categoryTreeResponse=new CategoryTreeResponse();
		
		try {
			
			CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if(cookieUser!=null){
					String phoneNum = cookieUser.getPhoneNumber();
					if(phoneNum!=null){
						List<Category> categoryTree = categoryService.getCategoryTree("0");
						categoryTreeResponse.setCode("1");
						categoryTreeResponse.setCategorys(categoryTree);
						}else {
							categoryTreeResponse.setCode("2");
							categoryTreeResponse.setDes("请重新登陆");
						}
				}else {
					categoryTreeResponse.setCode("2");
					categoryTreeResponse.setDes("请重新登陆");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(categoryTreeResponse));

		} catch (Exception e) {
			
			e.printStackTrace();
			categoryTreeResponse.setCode("0");
			categoryTreeResponse.setDes("系统繁忙");
		}
		
		
		return categoryTreeResponse;
	}
	

}
